package com.zz.yt.lib.bluetooth.delegate.temperature;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ThreadUtils;
import com.zz.yt.lib.bluetooth.R;
import com.zz.yt.lib.bluetooth.base.BaseBluetoothRxDelegate;
import com.zz.yt.lib.bluetooth.utils.TemperatureUtil;
import com.zz.yt.lib.ui.BundleKey;
import com.zz.yt.lib.ui.CustomTitleBar;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


/**
 * 获取温度
 *
 * @author qf
 * @version 1.0
 **/
public class TemperatureAddedDelegate extends BaseBluetoothRxDelegate {


    @NonNull
    public static TemperatureAddedDelegate create() {
        final Bundle args = new Bundle();
        final TemperatureAddedDelegate fragment = new TemperatureAddedDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Object setLayout() {
        return R.layout.blu_delegate_added_weight;
    }

    @Override
    protected void setCustomTitleBar(@NonNull CustomTitleBar titleBar) {
        titleBar.setText("温度");
    }

    @Override
    protected void getData(Bundle bundle) {
        super.getData(bundle);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        super.onBindView(savedInstanceState, rootView);
        if (mTextUnit != null) {
            mTextUnit.setText("℃");
        }
    }

    @Override
    protected void inReadThread() {
        ThreadUtils.executeByFixedAtFixRate(1, new ThreadUtils.SimpleTask<String>() {

            @Override
            public String doInBackground() {
                return "";
            }

            @Override
            public void onSuccess(@Nullable String result) {
                int bytes;
                byte[] buffer = new byte[1024];
                try {
                    if ((bytes = is.read(buffer)) > 0) {
                        byte[] bufData = new byte[bytes];
                        System.arraycopy(buffer, 0, bufData, 0, bytes);
                        if (mTextWeight != null) {
                            mTextWeight.setText(TemperatureUtil.getResultTemperature(
                                    TemperatureUtil.getHexString(bufData, 0, 0)));
                        }
                    }
                } catch (Exception e) {
                    bThread = false;
                    try {
                        is.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    //异常的时候break跳出无限循环
                    cancel();
                }
            }
        }, 1, TimeUnit.SECONDS);
    }


    /**
     * 点击确定
     */
    @Override
    protected void onResult(double mWeight) {
        final Bundle args = new Bundle();
        args.putDouble(BundleKey.TEXT.name(), mWeight);
        getSupportDelegate().setFragmentResult(Activity.RESULT_OK, args);
        getSupportDelegate().pop();
    }

}

